<template>
    <rx-layout class="data-source-layout">
        <div slot="center" style>
            <rx-fit :isheader="isheader">
                <div slot="toolheader" foldheader="true" foldbtn="false" border="false">
                    <div class="table-operator">
                        <a-button type="primary" icon="plus" @click="add">
                            {{ $ti18('rxList.newBtn', '新建') }}
                        </a-button>
                        <a-button v-if="editEnable && selectedRowKeys.length > 0" type="primary" icon="edit"
                                  @click="editOne">
                            {{ $ti18('rxList.editBtn', '编辑') }}
                        </a-button>
                        <a-button v-if="removeEnable && selectedRowKeys.length > 0" type="primary" icon="delete"
                                  @click="delByIds(selectedRowKeys)">
                            {{ $ti18('rxList.delBtn', '删除') }}
                        </a-button>
                        <a-button type="primary" icon="check" @click="testConnect">
                            {{ i18n('list.testCon', '测试连接') }}
                        </a-button>
                        <a-button v-if="selectedRowKeys.length > 0 && appMode!='single'" type="primary" icon="database"
                                  @click="publish(selectedRowKeys)">
                            {{ i18n('list.SyncToNACOS', '同步至NACOS') }}
                        </a-button>
                    </div>
                    <span class="search-btn-box" v-if="!isheader">
                    <span class="search-btn" @click.stop="searchshow"><i class="iconfont iconguolv"></i>
                        {{ $ti18('rxList.leachBtn', '过滤') }}
                    </span>
                    </span>
                </div>
                <div btnalign="right" slot="searchbar" v-if="fitSearch" @search="search" @cancel="cancel" @close="close">
                    <a-form :layout="colLayout">
                        <a-row :gutter="24">
                            <a-col :span="colSpan">
                                <a-form-item :label="i18n('form.datasource','数据源')+' '+i18n('form.name','名称')">
                                    <a-input
                                        :placeholder="i18n('form.datasource','数据源','input')+' '+i18n('form.name','名称')"
                                        v-model="queryParam.Q_NAME__S_LK"/>
                                </a-form-item>
                            </a-col>
                            <a-col :span="colSpan">
                                <a-form-item :label="i18n('form.alias','别名')">
                                    <a-input :placeholder="i18n('form.alias','别名','input')"
                                             v-model="queryParam.Q_ALIAS__S_LK"/>
                                </a-form-item>
                            </a-col>
                        </a-row>
                    </a-form>
                </div>
                <rx-grid
                    ref="table"
                    :bordered="isBorder"
                    :allowRowSelect="true"
                    :multiSelect="true"
                    :columns="columns"
                    :defaultPageSize="10"
                    url="/api-form/form/core/formDataSourceDef/query"
                    :queryParam="queryParam"
                    data-field="result.data"
                    :pageSizeOptions="['10', '30', '40']"
                    id-field="id"
                    @selectChange="onSelectChange"
                >
                    <span slot="name" slot-scope="{ text, record }">
                        <a-icon type="database"></a-icon>&nbsp;{{ text }}
                    </span>

                    <span slot="enable" slot-scope="{ text, record }">
                        <a-tag :color="enableMap[text].color">
                          <span>{{ enableMap[text].text }}</span>
                        </a-tag>
                    </span>

                    <span slot="initOnStart" slot-scope="{ text, record }">
                        <a-tag :color="initMap[text].color">
                          <span>{{ initMap[text].text }}</span>
                        </a-tag>
                    </span>
                    <span slot="action" slot-scope="{ text, record }" class="actionIcons" size="5">
                        <span v-if="editEnable" @click.stop="edit(record)">
                            {{ $ti18('rxList.editBtn', '编辑') }}
                        </span>
                        <span v-if="removeEnable" @click.stop="delByIds([record.id])">
                            {{ $ti18('rxList.delBtn', '删除') }}
                        </span>
                        <span @click.stop="testDbConnect(record.id)" :title="i18n('list.testCon','测试连接')">
                            {{ i18n('list.testCon', '测试连接') }}
                        </span>
                        <span @click.stop="share(record)">
                            {{ i18n('list.share', '共享') }}
                        </span>
                    </span>
                </rx-grid>
            </rx-fit>
        </div>
    </rx-layout>
</template>

<script>
import FormDataSourceDefApi from '@/api/form/core/formDataSourceDef'
import FormDataSourceDefEdit from './FormDataSourceDefEdit'
import {BaseList, Dialog, RxLayout, RxFit} from "@lowcode/jpaas-common-lib";
import {mapState} from 'vuex';

export default {
    name: 'FormDataSourceDefList',
    mixins: [BaseList],
    components: {
        RxLayout,
        RxFit,
    },
    computed: {
        ...mapState({
            appMode: state => state.appSetting.appMode,
        })
    },
    data() {
        return {
            columns: [
                {
                    title: this.$ti18('rxList.index', '序号'),
                    type: 'indexColumn',
                    width: 60,
                    dataIndex: 'serial',
                    scopedSlots: {customRender: 'serial'},
                },
                {
                    title: this.i18n('form.name', '名称'),
                    dataIndex: 'name',
                    width: '130%',
                    scopedSlots: {customRender: 'name'}
                },
                {title: this.i18n('form.alias', '别名'), dataIndex: 'alias', width: '120%'},
                {title: this.i18n('form.appName', '服务名'), dataIndex: 'appName', width: '120%'},
                {
                    title: this.i18n('form.enable', '启用'),
                    dataIndex: 'enable',
                    scopedSlots: {customRender: 'enable'},
                    width: '100%'
                },
                {title: this.$ti18('rxForm.createTime', '创建时间'), dataIndex: 'createTime', width: '140%'},
                {
                    title: this.$ti18('rxList.action', '操作'),
                    width: '160%',
                    dataIndex: 'action',
                    scopedSlots: {customRender: 'action'}
                },
            ],
            component: FormDataSourceDefEdit,
            comment: this.i18n('list.comment', '数据源定义管理'),
            widthHeight: ['1200px', '600px'],
            enableMap: {
                true: {color: 'green', text: this.i18n('form.enable', '启用')},
                false: {color: 'red', text: this.i18n('colunmMap.disEnable', '禁用')},
            },
            initMap: {
                true: {color: 'green', text: this.i18n('colunmMap.yes', '是')},
                false: {color: 'red', text: this.i18n('colunmMap.no', '否')},
            },
        }
    },
    created() {
        this.needFilterAppId();
        if (this.appMode == 'single') {
            //去掉应用名称
            this.columns.splice(3, 1);
        }
    },
    methods: {
        i18n(name, text, key) {
            return this.$ti18(name, text, "formDS", key);
        },
        publish(ids) {
            FormDataSourceDefApi.publish({ids: ids.join(',')})
        },
        delById(ids) {
            return FormDataSourceDefApi.del({ids: ids.join(',')})
        },
        getListByParams(parameter) {
            return FormDataSourceDefApi.query(parameter)
        },
        testConnect() {
            if (this.selectedRows.length > 0) {
                const pkId = this.selectedRows[0].pkId
                FormDataSourceDefApi.testConnect(pkId)
            }
        },
        testDbConnect(pkId) {
            FormDataSourceDefApi.testConnect(pkId)
        },
        search() {
            this.$refs.table.loadData();
        },
        cancel() {
            this.resetSearch();
        },
        //共享给其它应用
        share(record) {
            let self = this;
            FormDataSourceDefApi.getShareApps(record.id).then(res => {
                let initData = res.data;
                if (!initData) {
                    initData = [];
                }
                self.showAppDialog(initData, record.id);
            });
        },
        showAppDialog(initData, dsId) {
            Dialog.openSystemAppDialog({
                curVm: this, widthHeight: ['1150px', '735px'],
                data: {initSelected: initData, isDevelopApp: true, isSelectApp: true}
            }, function (self, data) {
                //保存共享信息
                FormDataSourceDefApi.saveShareApps(data.apps, dsId).then(res => {
                    if (res.success && res.code == 200) {
                    }
                });
            })
        },
    }
}
</script>
<style scoped>
.data-source-layout {
    background-color: #fff;
}
</style>

